package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseFragment
import com.hunktimes.wholesale.databinding.FragmentJewelListBinding
import com.hunktimes.wholesale.databinding.PageListBinding
import com.hunktimes.wholesale.extension.setImageUrl
import com.hunktimes.wholesale.mock.MockJsonParser.goods
import com.hunktimes.wholesale.net.model.MockGoods
import com.hunktimes.wholesale.ui.tabs.cart.CartRecommendAdapter
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.util.L

/**
 * Author: Delete
 * Date: 2020/9/16 17:45
 * Description:
 *
 */
class JewelListFragment : BaseFragment(R.layout.fragment_jewel_list) {
    companion object {
        fun newInstance(arg: Bundle): JewelListFragment {
            val fragment = JewelListFragment()
            fragment.arguments = arg
            return fragment
        }
    }

    lateinit var viewBinding: FragmentJewelListBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentJewelListBinding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        initJewels()
    }

    val jewelListAdapter = JewelListAdapter()
    fun initJewels() {
        val lm = GridLayoutManager(requireContext(), 2)
        lm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.recyclerView.layoutManager = lm
        viewBinding.recyclerView.adapter = jewelListAdapter
        val data = mutableListOf<MockGoods>()
        repeat(10) {
            data.add(goods.random())
        }
        jewelListAdapter.data = data
        jewelListAdapter.setOnItemChildClickListener { adapter, view, position ->
            L.d("jewelListAdapter.setOnItemChildClickListener")
            val bundle = bundleOf(Constants.key_goods_bundle to adapter.data[position])
            rootNavController.navigate(R.id.navigation_product, bundle)
        }
    }


}

class JewelListAdapter : BaseQuickAdapter<MockGoods, BaseViewHolder>(R.layout.item_recommend_v2) {
    override fun convert(holder: BaseViewHolder, item: MockGoods) {
//        val itemViewBinding = ItemRecommendBinding.bind(holder.itemView)
//        itemViewBinding.itemRecommendCoverIv.setImageUrl(item)
        val coverIv = holder.getView<ImageView>(R.id.item_recommend_cover_iv)
        coverIv.setImageUrl(item.imageUrl)
        holder.setText(R.id.item_recommend_title_tv, item.nameSafe)
        holder.setText(R.id.item_recommend_desc_tv, item.name2)
        holder.setText(R.id.item_recommend_price_tv, item.priceHK)
    }

}